<template>
  <div class="design-attribute">
    <!-- 配置导航 -->
    <div class="header">
      <div class="header-active" :style="{ left: offset }" />
      <div class="header-nav">
        <div
          class="nav-label"
          v-for="(item, index) in nav"
          :key="index"
          @click="handle(item)"
        >
          <span>{{ item.label }}</span>
        </div>
      </div>
    </div>
    <div class="design-panel">
      <div class="attribute panel" v-if="key == 0">
        <!-- 页面配置 -->
        <div class="shape-panel">
          <div class="complex-position-box">
            <div class="position-item page-config">
              <span class="label">宽度</span>
              <text-number v-model="page.width" />
            </div>
            <div class="position-item page-config">
              <span class="label">高度</span>
              <text-number v-model="page.height" />
            </div>
            <!-- <div class="position-item page-config">
              <span class="label">行数</span>
              <text-number v-model="page.layout.row" />
            </div> -->
            <!-- <div class="position-item page-config">
              <span class="label">缩放</span>
              <text-number v-model="page.scale" />
            </div> -->
            <!-- <div class="position-item page-config">
              <span class="label">列数</span>
              <text-number v-model="page.layout.column" />
            </div>
            <div class="position-item page-config">
              <span class="label">间隔</span>
              <text-number v-model="page.layout.gutter" />
            </div> -->
          </div>
        </div>
        <!-- 组件位置信息 -->
        <div class="shape-panel" v-if="complex">
          <div class="complex-label-box" v-if="config">
            <complex-label-input v-model="config.label" placeholder="组件名" />
          </div>
          <div class="complex-position-box">
            <div
              class="position-item position-complex-config"
              v-for="(item, index) in position"
              :key="index"
            >
              <span class="label">{{ item.label }}</span>
              <text-number v-model="complex[item.key]" />
            </div>
          </div>
        </div>
        <!-- 属性配置项 -->
        <div class="complex-config">
          <div class="attr-list" v-if="complex">
            <panel-collapse
              v-for="(group, index) in attrGroup"
              :key="index"
              :option="group"
              :count="count"
            >
              <div
                class="attr-item"
                v-for="(item, index) in group.child"
                :key="index"
              >
                <span
                  class="attr-label"
                  v-if="
                    (item.title && !item.hiddenTitle && !item.hidden) ||
                    item.hidden == null
                  "
                  >{{ item.title }}</span
                >
                <div class="attr-content">
                  <component
                    v-if="item.default"
                    :is="item.formType"
                    :value.sync="item.data"
                    :type="item.key"
                    :title="item.title"
                    :data="item.default"
                    @change="changeData"
                    @update="updateData"
                  />
                  <component
                    v-else
                    :is="item.formType"
                    :value.sync="item.data"
                    :type="item.key"
                    :title="item.title"
                    @change="changeData"
                    @update="updateData"
                  />
                </div>
              </div>
            </panel-collapse>
          </div>
        </div>
      </div>
      <!-- 数据来源 -->
      <div class="data-source panel" v-if="key == 1">
        <div class="data-source-content" v-if="config && config.type">
          <div class="data-source-form">
            <div class="data-source-label">请求方式</div>
            <div class="content">
              <vxe-select
                v-model="config.type"
                placeholder="请求方式"
                size="mini"
                style="width: 100%"
              >
                <vxe-option
                  v-for="(item, index) in sourceList"
                  :key="index"
                  :value="item.value"
                  :label="item.label"
                />
              </vxe-select>
            </div>
          </div>
          <div class="data-source-form" v-if="config.type === 'api'">
            <div class="data-source-label">接口地址</div>
            <div class="content">
              <input-option
                v-model="config.url"
                placeholder="请求地址"
              ></input-option>
            </div>
          </div>
          <div class="data-source-form" v-if="config.type === 'api'">
            <div class="data-source-label">接口类型</div>
            <div class="content">
              <select-option-wrap
                v-model="axios.type"
                :data="[
                  {
                    value: 'get',
                    label: 'GET',
                  },
                  {
                    value: 'post',
                    label: 'POST',
                  },
                ]"
              ></select-option-wrap>
            </div>
          </div>
          <div class="data-source-form" v-if="config.type === 'api'">
            <div class="data-source-label">请求头</div>
            <div class="content">
              <input-option
                v-model="axios.header"
                placeholder="请求头"
              ></input-option>
            </div>
          </div>
          <div class="data-source-form" v-if="config.type === 'datasource'">
            <div class="data-source-label">数据集</div>
            <div class="content">
              <vxe-select
                v-model="config.url"
                placeholder="选择数据集"
                size="mini"
                style="width: 100%"
              >
                <vxe-option
                  v-for="(item, index) in dataSource"
                  :key="index"
                  :value="item.value"
                  :label="item.label"
                />
              </vxe-select>
            </div>
          </div>
        </div>
        <div class="no-data" v-else>当前组件不支持</div>
      </div>
      <!-- 事件 -->
      <div class="active-bind panel" v-if="key == 2">
        <div class="active-content" v-if="complex">
          <div class="active-create" @click="createEvent">
            <span>添加事件</span>
          </div>
          <div
            class="active-item"
            v-for="(active, index) in configActive"
            :key="index"
          >
            <div class="active-title">
              <span>事件 {{ index + 1 }}</span>
              <span @click="deleteEvent(index)">
                <svg-icon class="svg-icon" icon="delete" />
              </span>
            </div>
            <div class="active-form">
              <div class="active-label">选择事件</div>
              <div class="content">
                <vxe-select
                  v-model="active.event"
                  placeholder="交互事件"
                  size="mini"
                  style="width: 100%"
                >
                  <vxe-option
                    v-for="(item, index) in eventList"
                    :key="index"
                    :value="item.value"
                    :label="item.label"
                  />
                </vxe-select>
              </div>
            </div>
            <div class="active-form">
              <div class="active-label">目标组件</div>
              <div class="content">
                <vxe-select
                  v-model="active.bindId"
                  placeholder="目标组件"
                  size="mini"
                  style="width: 100%"
                  @change="attr"
                >
                  <vxe-option
                    v-for="(item, index) in list"
                    :key="index"
                    :value="item.id"
                    :label="item.title"
                  />
                </vxe-select>
              </div>
            </div>
            <div class="active-form" v-if="attrList.length > 0">
              <div class="active-label">目标属性</div>
              <div class="content">
                <vxe-select
                  v-model="active.reslut"
                  placeholder="目标属性"
                  size="mini"
                  style="width: 100%"
                  @change="attr"
                >
                  <vxe-option
                    v-for="(item, index) in attrList"
                    :key="index"
                    :value="item.key"
                    :label="item.title"
                  />
                </vxe-select>
              </div>
            </div>
            <div class="active-form" v-if="attrList.length > 0">
              <div class="active-label">行为参数</div>
              <div class="content">
                <input-option
                  v-model="active.value"
                  placeholder="参数值"
                ></input-option>
              </div>
            </div>
          </div>
        </div>
        <div class="no-data" v-else>当前组件不支持</div>
      </div>
    </div>
  </div>
</template>

<script>
import attrcomponent from "./components";
import select from "@/components/select-option-wrap";
import input from "@/components/input-option";
import panel from "@/components/panel-collapse";
import ComplexLabelInput from "./components/complex-label-input";
import { mapState } from "vuex";
export default {
  components: {
    ...attrcomponent,
    "select-option-wrap": select,
    "input-option": input,
    "panel-collapse": panel,
    ComplexLabelInput,
  },
  props: {
    page: {
      type: Object,
    },
  },
  data() {
    return {
      count: 0,
      key: 0,
      nav: [
        {
          key: 0,
          label: "基础属性",
        },
        {
          key: 1,
          label: "数据来源",
        },
        {
          key: 2,
          label: "交互事件",
        },
      ],
      // 位置信息
      position: [
        {
          label: "W",
          key: "w",
        },
        {
          label: "H",
          key: "h",
        },
        {
          label: "X",
          key: "x",
        },
        {
          label: "Y",
          key: "y",
        },
        {
          label: "Z",
          key: "z",
        },
        {
          label: "R",
          key: "r",
        },
      ],
      sourceList: [
        {
          value: "table",
          label: "默认",
        },
        {
          value: "datasource",
          label: "数据集",
        },
        {
          value: "api",
          label: "API接口",
        },
        {
          value: "json",
          label: "JSON",
        },
      ],
      dataSource: [],
      eventList: [
        {
          value: "onclick",
          label: "单击",
        },
        {
          value: "dblclick",
          label: "双击",
        },
      ],
      // 接口请求数据配置项
      axios: {
        type: "get",
        header: "application/javascript; charset=utf-8",
      },
      option: {
        value: "id",
        label: "title",
      },
      attrList: [],
      attroption: {
        value: "key",
        label: "title",
      },
    };
  },
  computed: {
    ...mapState({
      list: (state) => state.design.complexData,
      complex: (state) => state.design.selectComplex,
    }),
    offset() {
      return `${this.key * 69 + 16}px`;
    },
    config() {
      const complex = this.$store.state.design.selectComplex;
      return complex ? complex.config : null;
    },
    configAttr() {
      const config = this.$store.state.design.selectComplex.config;
      return config ? config.attr : [];
    },
    attrGroup() {
      return this.group();
    },
    configActive() {
      const config = this.$store.state.design.selectComplex.config;
      return config ? config.active : [];
    },
  },

  methods: {
    group() {
      // 属性分组
      const attr = this.$store.state.design.selectComplex.config.attr;
      if (!attr) return;
      const group = this.$store.state.design.group;
      group.map((item) => {
        const child = attr.filter((e) => item.key === e.group);
        item.child = child;
      });
      this.count++;
      return group;
    },
    attr(data) {
      console.log(data.value);
      // 获取组件的全部属性
      const find = this.list.find((item) => item.id == data.value);
      if (find) {
        this.attrList = find.config.attr;
      }
    },
    handle(item) {
      this.key = item.key;
    },
    deleteEvent(index) {
      // 删除交互事件
      this.config.active.splice(index, 1);
    },
    createEvent() {
      // 新建交互事件
      if (this.config.active) {
        this.config.active.push({
          bindId: null, // 绑定操作组件
          event: null,
          reslut: null, // 操作事件属性
          value: null,
          params: null,
          describe: "事件描述",
        });
      } else {
        console.log("当前组件不支持交互事件");
      }
    },
    // 修改属性
    changeData(data) {
      const { key, value } = data;
      this.config.attr.map((item) => {
        if (item.key == key) {
          item.data = value;
        }
        return item;
      });
    },
    // 修改组件宽高 栅格布局化
    updateData(data) {
      const width = (1920 / 4) * data.row;
      const height = (1080 / 4) * data.column;
      this.complex.w = width;
      this.complex.h = height;
    },
  },
};
</script>

<style lang="scss" scoped>
.data-source {
  width: 100%;
  padding: 6px 10px;
  box-sizing: border-box;
}

.data-source-content {
  width: 100%;
}

.data-source-form {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 6px auto 10px;

  .content {
    // flex: 1;
    width: calc(100% - 69px);
  }

  .data-source-label {
    width: 69px;
    text-align: right;
    font-size: 14px;
    margin-right: 6px;
  }
}
</style>

<style lang="scss" scoped>
.active-bind {
  width: 100%;
  height: 100%;
  padding: 6px 16px;
  box-sizing: border-box;

  .active-create {
    width: 100%;
    padding: 4px;
    background-color: #409eff;
    border-radius: 2px;
    cursor: pointer;
    font-size: 12px;
    color: #fff;
    text-align: center;
    margin-bottom: 6px;
  }

  .active-content {
    width: 100%;
    height: calc(100% - 60px);
    overflow-y: auto;
    overflow-x: hidden;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  .active-item {
    width: 100%;
    padding: 4px 6px;
    background-color: rgb(247, 247, 247);
    margin-bottom: 6px;
    border-radius: 2px;
    transition: all 0.2s ease-in-out 0s;

    .active-title {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 13px;
      padding: 6px 0;
      border-bottom: 1px solid rgb(219, 219, 219);

      .svg-icon {
        width: 20px;
        cursor: pointer;

        &:hover {
          color: #409eff;
        }
      }
    }

    .active-form {
      width: 100%;
      height: 32px;
      font-size: 12px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 6px auto 4px;
    }

    .content {
      flex: 1;
      width: calc(100% - 69px);
      margin-left: 6px;
    }

    .active-label {
      width: 50px;
      text-align: right;
    }
  }
}
</style>

<style lang="scss" scoped>
.design-attribute {
  width: 100%;
  height: 100%;

  .header {
    position: relative;
    width: 100%;
    height: 36px;
    padding: 0 16px;
    box-sizing: border-box;
    border-bottom: 1px solid rgb(219, 219, 219);

    .header-active {
      position: absolute;
      left: 207px;
      bottom: 0px;
      width: 30%;
      border-bottom: 2px solid #409eff;
      transition: all 0.28s ease;
    }

    .header-nav {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;

      .nav-label {
        flex: 1;
        color: #000;
        text-align: center;
        font-size: 14px;
        cursor: pointer;
      }
    }
  }

  .design-panel {
    width: 100%;
    height: calc(100% - 36px);

    .panel {
      width: 100%;
    }

    .attribute {
      width: 100%;
      height: 100%;
    }
  }
}

.complex-label-box {
  width: 100%;
  display: flex;
  padding: 6px 16px;
  box-sizing: border-box;
  border-bottom: 1px solid rgb(219, 219, 219);
}

.complex-position-box {
  width: 100%;
  padding: 6px 16px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  border-bottom: 1px solid rgb(219, 219, 219);

  .position-item {
    height: 20px;
    line-height: 20px;
    font-size: 12px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin: 6px 0;
  }

  .page-config {
    width: 50%;
  }

  .page-config .label {
    display: inline;
    text-align: center;
    min-width: 32px;
  }

  .position-complex-config {
    width: 50%;
  }

  .position-complex-config .label {
    display: inline;
    width: 35px;
    text-align: center;
  }
}
</style>

<style scoped lang="scss">
.complex-config {
  width: 100%;
  height: calc(100% - 220px);
  overflow-y: auto;
  overflow-x: hidden;
}

.attr-list {
  width: 100%;

  .attr-item {
    width: 100%;
    margin: 10px auto;
    display: flex;
    justify-content: center;
    align-items: center;

    .attr-label {
      margin-right: 6px;
      height: 24px;
      line-height: 24px;
      min-width: 69px;
      font-size: 12px;
      text-align: right;
    }

    .attr-content {
      flex: 1;
      min-width: calc(100% - 69px);
    }
  }
}

.no-data {
  font-size: 14px;
  text-align: center;
  color: #666;
}
</style>
